package zq.cn.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;
import java.util.Map;

import utils.Globals;
import utils.Images;
import adapter.MyAdapter;
import utils.TableDao;

public class MainActivity extends AppCompatActivity {

    private LinearLayout topBgImg;
    private TextView topImg;
    private GridView grid;
    private List<Map<String, Object>> values;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        topBgImg = (LinearLayout) findViewById(R.id.top_bg_img);
        topImg = (TextView) findViewById(R.id.top_image);
        grid = (GridView) findViewById(R.id.grid);

        topBgImg.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getTopBackground()));
        topImg.setBackgroundDrawable(new BitmapDrawable(getResources(), Images.getTopImg()));
        int nowStage = getSharedPreferences("game",MODE_PRIVATE).getInt("nowStage", 0);
        topImg.setText(String.valueOf(nowStage));

        values = TableDao.getAllData(nowStage);
        adapter = new MyAdapter(this, values);
        grid.setAdapter(adapter);

        //设置事件，监控grid，进入游戏界面,改gridview为选关界面
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = values.get(position);
                if((boolean)map.get("flag")) {
                    Intent in = new Intent(MainActivity.this, GameActivity.class);
                    in.putExtra("stageNum", (int)map.get("stageTxt"));
                    startActivity(in);
                }else {
                    Toast.makeText(MainActivity.this, "",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
